var app = getApp();

Page({
  data: {
    motto: 'Hello World',
    showModal: false,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      code: options.code
    })
  },
  bindGetUserInfo: function (e) {
    var that = this;
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            withCredentials: true,
            success: function (res_user) {
              var jsonData = {
                code: that.data.code,
                encryptedData: res_user.encryptedData,
                iv: res_user.iv
              };
              app.JsonPost('Api/User/login', jsonData, function (data) {
                console.log(data);
                if (data.mobile == null || data.identity == null || data.real_name == '') {
                  wx.setStorageSync("certification", "false");
                } else {
                  wx.setStorageSync("certification", "true");
                }
                //存储用户信息
                wx.setStorageSync("userId", data.id);
                wx.setStorageSync("userName", data.user_name);
                wx.setStorageSync("realName", data.real_name);
                wx.setStorageSync("identity", data.identity);
                wx.setStorageSync("avatar", data.avatar);
                wx.setStorageSync("gender", data.gender);
                wx.setStorageSync("credit", data.credit);
                wx.setStorageSync("mobile", data.mobile == null ? '' : data.mobile);
                wx.setStorageSync("wxid", data.weixin == null ? '' : data.weixin);
                wx.switchTab({
                  url: '../../page/index/index'
                })
              })
            },
            fail: function () {
              console.log('获取用户信息失败')
            }
          })
        }
      }
    })
  }

})